package com.ruoyi.project.tool;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 212行以下为该类注释部分，需要使用可取消注释开启使用 务必为每个方法加注释
 * <p>
 * Title: 江西省中国银行运营集中管理系统
 * </p>
 * <p>
 * Description: 日期时间工具类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: 京北方科技股份有限公司
 * </p>
 * 
 * @author 吴练
 * @version 1.0
 */
public class DateTimeUtil {

	public static final String FMT_YY_MM_DD = "yyyy-MM-dd";
	public static final String FMT_YY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String FMTYYMMDD_HH_MM_SS = "yyyyMMdd HH:mm:ss";
	public static final String FMTYYMMDD_HH_MM = "yyyyMMdd HH:mm";
	public static final String FMT_YYMMDD = "yyyyMMdd";
	public static final String YYYY = "yyyy";
	public static final String FMT_YYMMDDHHMMSS = "yyyyMMddHHmmss";
	private static SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
	// ////////////////////以下返回String型//////////////////////////////////////
	/**
	 * 返回字符串,指定格式的日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String shortFmt(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static java.sql.Date getDateFormat(){
		return parseDate_FMT_YY_MM_DD(dateFmt(new java.sql.Date(System.currentTimeMillis())));
	}
	public static java.sql.Date getDateFormat(Date date){
		if(date!=null){
			return parseDate_FMT_YY_MM_DD(DateTimeUtil.dateFmt(date));
		}else{
			return parseDate_FMT_YY_MM_DD(dateFmt(new java.sql.Date(System.currentTimeMillis())));
		}
	}
	/**
	 * 返回字符串,指定格式的当前日期
	 * 
	 * @return
	 */
	public static String getDate() {
		return shortFmt(new Date(), FMT_YY_MM_DD);
	}

	public static String getYYMMDDDate() {
		return shortFmt(new Date(), FMT_YYMMDD);
	}

	public static String getDate(String format) {
		return shortFmt(new Date(), format);
	}
	public static String getYYMMDDHHMMSSDate() {
		return shortFmt(new Date(), FMT_YYMMDDHHMMSS);
	}
	
	/**
	 * 返回字符串,yyyy-MM-dd，指定日期
	 * 
	 * @param date
	 * @return
	 */
	public static String dateFmt(Date date) {
		return shortFmt(date, FMT_YY_MM_DD);
	}
	/**
	 * 获取指定日期的前interval天(interval 为负数) -1 前一天 或者后interval天(interval 为正数) 1 后一天
	 * 
	 * @param date
	 * @param interval
	 * @return
	 */
	public static String getBeforeOrAfterDate(Date date, int interval,
			String format) {
		Calendar currentTime = Calendar.getInstance();
		currentTime.setTime(date);
		currentTime.add(Calendar.DATE, interval);
		return shortFmt(currentTime.getTime(), format);
	}

	/**
	 * 获取当前时间的上一天
	 * 
	 * @return
	 */
	public static String getYesterday() {
		return getBeforeOrAfterDate(new Date(), -1, FMT_YY_MM_DD);
	}

	public static String getYesterday_YYYYMMDD() {
		return getBeforeOrAfterDate(new Date(), -1, FMT_YYMMDD);
	}

	public static String getNextDay(String date) {
		return getBeforeOrAfterDate(parseShort(date), 1, FMT_YY_MM_DD);
	}

	// ////////////////////以下返回java.util.Date型///////////////////////////////////
	// ///
	/**
	 * 返回日期型，指定格式的日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date parse(String strDate, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		if (null != strDate && !"".equals(strDate)) {
			try {
				date = sdf.parse(strDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

	/**
	 * 返回日期型，yyyy-MM-dd HH:mm:ss格式，指定日期
	 * 
	 * @param param
	 * @return
	 */
	public static Date parse(String param) {
		return parse(param, FMT_YY_MM_DD_HH_MM_SS);
	}
	
	public static Date parse2(String param) {
		return parse(param, FMTYYMMDD_HH_MM_SS);
	}

	/**
	 * 返回日期型，yyyy-MM-dd格式，指定日期
	 * 
	 * @param param
	 * @return
	 */
	public static Date parseShort(String param) {
		return parse(param, FMT_YY_MM_DD);
	}

	/**
	 * 
	 * @param param
	 * @return
	 */

	public static String getYear() {
		Calendar ca = Calendar.getInstance();
		ca.setTime(new Date());
		String year = "" + ca.get(Calendar.YEAR);
		return year;
	}

	public static String getSimpleYear() {
		return getYear().substring(2);
	}
	
	public static int minusDate(Date date1,Date date2){
		int days = 0;
		Calendar ca = Calendar.getInstance();
		ca.setTime(date1);
		int day1 = ca.get(Calendar.DAY_OF_YEAR);
		ca.setTime(date2);
		int day2 =  ca.get(Calendar.DAY_OF_YEAR);
		days = day1-day2;
		return days;
	}

	// ////////////////////以下返回java.sql.Date型////////////////////////////////////
	// //

	public static java.sql.Date parseDate(String param) {
		return parseSqlDate(param, FMT_YYMMDD);
	}

	public static java.sql.Date parseDate_FMT_YY_MM_DD(String param) {
		return parseSqlDate(param, FMT_YY_MM_DD);
	}

	public static java.sql.Date getDateYesterday() {
		return parseSqlDate(getYesterday(), FMT_YY_MM_DD);
	}

	// ////////////////////以下返回java.sql.Timestamp型///////////////////////////////
	// ///////
	public static Timestamp parseTime_yyyyMMddHHmm(String param) {
		return new Timestamp(parseSqlDate(param, FMTYYMMDD_HH_MM).getTime());
	}
	public static Timestamp parseTime(String param) {
		return new Timestamp(parseSqlDate(param, FMT_YYMMDD).getTime());
	}

	public static Timestamp parseTime2(String param) {
		return new Timestamp(parseSqlDate(param, FMT_YYMMDD).getTime());
	}

	public static Timestamp parseTime3(String param) {
		return new Timestamp(parseSqlDate(param, FMT_YY_MM_DD).getTime());
	}
    public static Timestamp getFirstDayOfYearByTime(String time){
    	return parseTime3(time.substring(0,4)+"-01-01");
    }

	/**
	 * 
	 * @param strDate
	 * @param format
	 * @return
	 */
	public static java.sql.Date parseSqlDate(String strDate, String format) {
		if(strDate==null || strDate.trim().length()==0 ){
			return null;
		}
		return new java.sql.Date(parse(strDate, format).getTime());
	}

	// ////////////////////以下返回INT型//////////////////////////////////////

	/**
	 * 返回某年某月的天数
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDays(int year, int month) {
		int[] numberMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int numberOfMonth = numberMonth[month - 1];
		if (month == 2
				&& ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))) {
			numberOfMonth++;
		}
		return numberOfMonth;
	}
	
	/**
	 * 时间差
	 * @param date1 最近日期
	 * @param date2 最远日期
	 * @return
	 */
	public long minusDate(java.sql.Date date1,java.sql.Date date2){
	    long longReturn = 999;  
	    try{
	    	longReturn=((date1.getTime()-date2.getTime())/(1000 * 60 * 60 *24));
	      //System.out.println(longReturn);
	    }catch(Exception ex){
	      ex.printStackTrace();
	    }
	    return longReturn;
	  }
	
	/**
	 * 当前时间与指定日期的间隔天数
	 * @param date2
	 * @return
	 */
	public static long minusDate(java.sql.Date date2){
	    long longReturn = 999;  
	    try{
	    	longReturn=((System.currentTimeMillis()-date2.getTime())/(1000 * 60 * 60 *24));
	      //System.out.println(longReturn);
	    }catch(Exception ex){
	      ex.printStackTrace();
	    }
	    return longReturn;
	  }

	/**
	 * 获取指定日期的年份
	 * 
	 * @param date
	 * @return
	 * @throws NumberFormatException
	 */
	public static int getYear(String date) throws NumberFormatException {
		String m = date.substring(0, 4);
		return Integer.parseInt(m);
	}
	/**
	 * 获取当前日期年份
	 * @return
	 * @throws NumberFormatException
	 */
	public static int getCurYear()throws NumberFormatException {
		String m = getDate().substring(0, 4);
		return Integer.parseInt(m);
	}

	/**
	 * 获取指定日期的月份
	 * 
	 * @param date
	 * @return
	 * @throws NumberFormatException
	 */
	public static int getMonth(String date) throws NumberFormatException {
		String m = date.substring(date.indexOf("-") + 1, date.indexOf("-") + 3);
		return Integer.parseInt(m);
	}

	/**
	 * 获取指定日期的天
	 * 
	 * @param date
	 * @return
	 * @throws NumberFormatException
	 */
	public static int getDay(String date) throws NumberFormatException {
		String m = date.substring(date.lastIndexOf("-") + 1);
		return Integer.parseInt(m);
	}

	/**
	 * ������ӡ�
	 * 
	 * @param src
	 * @param add
	 * @return
	 */
	public static Date dayAdd(Date src, int add) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(src);
		ca.set(Calendar.DAY_OF_YEAR, ca.get(Calendar.DAY_OF_YEAR) + add);
		return ca.getTime();
	}

	/**
	 * ȡ�õ�ǰ����,���ҷ��غ�}�������. �����������,������,������ӳ�
	 * 
	 * @return
	 */
	public static String getTheDateAfter2days() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar xx = Calendar.getInstance();
		int i = 0;
		while (i < 2) {
			xx.add(Calendar.DATE, 1);
			if ((xx.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY)
					&& (xx.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)) {
				i++;
			}
		}
		sdf.format(xx.getTime());
		return sdf.format(xx.getTime());
	}

	/**
	 * 判断业务日期是否在当前月或当前月的上一月之间，风险表分表使用
	 * 
	 * @return 是：返回true 否：返回false
	 */
	public static boolean checkTrDate(java.sql.Date trDate) {
		// 当前时间
		Date currentDate = new Date();

		// 当月最后一天
		Calendar cal = Calendar.getInstance();
		cal.setTime(currentDate);
		int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, value);
		java.sql.Date lastDayOfMonth = new java.sql.Date(cal.getTime()
				.getTime());

		// 当月的上月第一天
		cal.add(Calendar.MONTH, -1);
		Date theDay = cal.getTime();
		cal.setTime(theDay);
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(theDay);
		gc.set(Calendar.DAY_OF_MONTH, 1);
		java.sql.Date firstDayOfLastMonth = new java.sql.Date(gc.getTime()
				.getTime());

		// 比较
		if (trDate.compareTo(lastDayOfMonth) <= 0
				&& trDate.compareTo(firstDayOfLastMonth) >= 0) {
			return true;
		}
		return false;
	}

	public static Date getBeginOfMonthDay(Date date) {
		String s = f.format(date);
		
		try {
			return f.parse(s.substring(0,6) + "01");
		} catch (ParseException e) {
			return date;
		}
	}
	public static Date getBeginOfNextMonthDay(Date date) {
		String s = f.format(date);
		int year = Integer.valueOf(s.substring(0, 4)).intValue();
		int month = Integer.valueOf(s.substring(4, 6)).intValue();
		int nextYear = year + 1;
		int nextMonth = month == 12 ? 1 : (month + 1);
		String beginNextMonth = (month == 12 ? nextYear : year) + ""
				+ (nextMonth > 9 ? (nextMonth + "") : ("0" + nextMonth)) + "01";
		
		try {
			return f.parse(beginNextMonth);
		} catch (ParseException e) {
			return date;
		}
	}
	
	/**
	 * 日期加上设定天数
	 * */
	public static String dayMouthAdd(java.sql.Date src, int add) {
	  Calendar ca = Calendar.getInstance();
	  ca.setTime(src);
	  ca.set(Calendar.DAY_OF_MONTH, ca.get(Calendar.DAY_OF_MONTH) + add);
	  DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	  String test = df.format(ca.getTime());
	  return test;
	}
	
	public static java.sql.Date parseTimestampToDate(Timestamp time, String format) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return new java.sql.Date(sdf.parse(time.toString()).getTime());
	}
	
	public static int compareYYYYMMDD(Date date1, Date date2){
		if(date1 == date2){
			return 0;
		}
		if(null == date1){
			return -1;
		}
		if(null == date2){
			return 1;
		}
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date1);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(date2);
		int compareY = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
		if(compareY != 0){
			return compareY;
		}
		int compareM = c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
		if(compareM != 0){
			return compareM;
		}
		int compareD = c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
		return compareD;
	}


	

	public static void main(String[] args){
		//System.out.println(getSimpleYear());
		//System.out.println(DateTimeUtil.parse("20110910 19:30", DateTimeUtil.FMTYYMMDD_HH_MM));
		// //System.out.println(getBeforeOrAfterDate(new Date(), -15,
		// FMT_YYMMDD));

	}
}
